package me.numark.view;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import butterknife.ButterKnife;
import butterknife.InjectView;
import me.numark.ArticleActivity;
import me.numark.R;
import me.numark.model.Feed;
import me.numark.model.News;
import me.numark.utils.Constants;
import me.numark.utils.Utils;

/**
 * This class contains all butterknife-injected Views & Layouts from layout file
 * 'feed_list_item2.xml'
 * for easy to all layout elements.
 *
 * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers
 *         (http://github.com/avast)
 */
public class FeedListItemViewHolder implements OnClickListener {

    @InjectView(R.id.avatar)
    public ImageView avatar;

    @InjectView(R.id.name)
    public TextView name;

    @InjectView(R.id.date)
    public TextView date;

    @InjectView(R.id.pic)
    public ImageView pic;

    @InjectView(R.id.title)
    public TextView title;

    @InjectView(R.id.news_src)
    public TextView newsSrc;

    @InjectView(R.id.mark_count)
    public TextView markCount;

    @InjectView(R.id.watch_count)
    public TextView watchCount;

    @InjectView(R.id.mark_text)
    public TextView markText;

    @InjectView(R.id.like_count)
    public TextView likeCount;

    @InjectView(R.id.comment_count)
    public TextView commentCount;

    @InjectView(R.id.forward_count)
    public TextView forwardCount;

    @InjectView(R.id.more)
    public ImageView more;

    @InjectView(R.id.news_view)
    public View newsView;

    @InjectView(R.id.news_sharer)
    TextView newsSharer;

    @InjectView(R.id.mark_area)
    View markArea;

    private Activity mActivity;

    public FeedListItemViewHolder(Fragment fragment, View view) {
        ButterKnife.inject(this, view);
        mActivity = fragment.getActivity();
        initViews();
    }

    public FeedListItemViewHolder(Activity activity, View view) {
        ButterKnife.inject(this, view);
        mActivity = activity;
        initViews();
    }

    private void initViews() {
        name.setOnClickListener(this);
        markCount.setOnClickListener(this);
        likeCount.setOnClickListener(this);
        commentCount.setOnClickListener(this);
        newsView.setOnClickListener(this);
        more.setOnClickListener(this);
    }

    private Feed mFeed;

    public void bindData(Feed feed) {
        mFeed = feed;
        Picasso.with(mActivity).load(feed.user.pic).into(avatar);
        name.setText(feed.user.nick);
        date.setText(Utils.getDatetimeStr(feed.create_timestamp * 1000));
        if (TextUtils.isEmpty(feed.news.face_img)) {
            pic.setImageResource(R.drawable.ic_feed_default_news_pic);
        } else {
            Picasso.with(mActivity).load(feed.news.face_img).into(pic);
        }
        title.setText(feed.news.title);
//        newsSrc.setText(feed.news.from_str + " · ");
//        newsSharer.setText(feed.news.by_user.nick);
        markCount.setText(Integer.toString(feed.news.topic_num));
//        watchCount.setText(Integer.toString(feed.news.read_num));
        markText.setText(feed.topic.topic);
        if (TextUtils.isEmpty(feed.topic.topic)) {
            markArea.setVisibility(View.GONE);
        } else {
            markArea.setVisibility(View.VISIBLE);
        }
        likeCount.setText(feed.like == 0 ? mActivity.getString(R.string.like) : Integer.toString(feed.like));
        commentCount.setText(feed.comments == 0 ? mActivity.getString(R.string.comment_0) : Integer.toString(feed.comments));
        forwardCount.setText(feed.forward == 0 ? mActivity.getString(R.string.forward_0) : Integer.toString(feed.forward));
        likeCount.setCompoundDrawablesWithIntrinsicBounds(
                feed.is_like ? R.drawable.ic_feed_like : R.drawable.ic_feed_unlike, 0, 0, 0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.name:
                break;
            case R.id.mark_count:
                break;
            case R.id.comment_count:
                break;
            case R.id.news_view: {
                News news = mFeed.news;
                Intent intent = new Intent(mActivity, ArticleActivity.class);
                intent.putExtra(Constants.NEWS_EXTRA, news.toString());
                intent.putExtra(Constants.SHARE_COUNT, news.share_count);
                mActivity.startActivity(intent);
            }
            break;
            case R.id.like_count:
                break;
            case R.id.more:
                break;
        }
    }
}
